<template>
	<view class="retrieve_password_main">
		<view class="account_info">
			<view class="account_info_title">
				手机号
			</view>
			<view class="account_info_input">
				<input type="text" v-model="formInline.phone" value="" placeholder-style="font-size:14px;color:#999999;" placeholder="请输入手机号" />
			</view>
		</view>
		<view class="account_info pr">
			<view class="account_info_title">
				验证码
			</view>
			<view class="account_info_input">
				<input type="text" value="" v-model="formInline.phoneCaptcha" placeholder-style="font-size:14px;color:#999999;" placeholder="请输入验证码" />
			</view>
			<span class="captcha dib tc pa" @click="smsCaptcha()">{{codeText}}</span>
		</view>
		<view class="account_info">
			<view class="account_info_title">
				新密码
			</view>
			<view class="account_info_input">
				<input type="text" value="" v-model="formInline.password" placeholder-style="font-size:14px;color:#999999;" placeholder="请输入新密码" />
			</view>
		</view>
		<view class="account_info">
			<view class="account_info_title">
				新密码
			</view>
			<view class="account_info_input">
				<input type="text" value="" v-model="formInline.confirmPassWord" placeholder-style="font-size:14px;color:#999999;" placeholder="请再次输入新密码" />
			</view>
		</view>
		<view class="btns register_btn" @click="submit()">
			找回
		</view>
	</view>
</template>

<script>
	import { isvalidPhone, isvalidPassword } from '@/utils/validate.js'
	export default {
		data() {
			return {
				formInline: {
					/** 登录手机号 */
					phone: '',
					/** 短信验证码 */
					phoneCaptcha: '',
					/** 登录密码 */
					password: '',
					/** 确认密码 */
					confirmPassWord: '',
					/** 邀请码 */
					referralCode: '',
				},
				codeText: '获取验证码',
			}
		},
		onLoad() {
		},
		
		components: {
		},
		
		methods: {
			/** 注册 */
			submit() {
				/** 手机号验证 */
				if (!isvalidPhone(this.formInline.phone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none',
						duration: 3000,
						position: 'bottom'
					});
					return
				}
				/** 短信验证码验证 */
				if (!this.formInline.phoneCaptcha) {
					uni.showToast({
						title: '请输入短信验证码',
						icon: 'none',
						duration: 3000,
						position: 'bottom'
					});
					return
				}
				/** 密码验证 */
				if (!isvalidPassword(this.formInline.password)) {
					uni.showToast({
						title: '请输入(6-16)位由数字和字母组成的密码',
						icon: 'none',
						duration: 3000,
						position: 'bottom'
					});
					return
				}
				/** 确认密码 */
				if (!this.formInline.confirmPassWord) {
					uni.showToast({
						title: '请再次确认密码',
						icon: 'none',
						duration: 3000,
						position: 'bottom'
					});
					return
				}
				/** 判断两次密码是否一致 */
				if (this.formInline.password !== this.formInline.confirmPassWord) {
					console.log(this.formInline);
					uni.showToast({
						title: '两次输入密码不一致',
						icon: 'none',
						duration: 3000,
						position: 'bottom'
					});
					return
				}
				let parameter = {
					mobile: this.formInline.phone, // 手机号
					password: this.formInline.password, // 密码
					repassword: this.formInline.confirmPassWord, // 确认密码
					captcha: this.formInline.phoneCaptcha, // 验证码
				}
				this.$post(this.api.forgetPassword, parameter).then((res) => {
					console.log(res)
					// /** 关闭当前页，返回至登录页 */
					// uni.navigateBack({
					//   delta: 1
					// });
				}).catch((err) => {
					console.log(err)
				})
			},
			/** 获取验证码 */
			smsCaptcha() {
				if (this.codeText !== '获取验证码') {
					return
				}
				
				if (!isvalidPhone(this.formInline.phone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none',
						duration: 3000,
						position: 'bottom'
					});
					return
				}
				/** 短信验证码倒计时 */
				let temp = 60
				this.codeText = temp + 's'
				let intervalFunc = setInterval(() => {
					if ( temp === 0 ) {
						temp = 0
						this.codeText = '获取验证码'
						clearInterval(intervalFunc)
					} else {
						temp -= 1
						this.codeText = temp + 's'
					}
				}, 1000)
				let parameter = {
					mobile: this.formInline.phone, // 手机号
					ip: '123456', // ip地址
					imei: '12345', // 手机唯一识别码
					device: '214325', // 手机设备信息
					type: 2, // 找回密码
				}
				this.$post(this.api.getVerification, parameter).then((res) => {
					console.log(res)
				}).catch((err) => {
					if (!res.success) {
						temp = 0
						this.codeText = '获取验证码'
						clearInterval(intervalFunc)
					}
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	@import "@/asset/less/base.less";
	.retrieve_password_main {
		padding: 68rpx 60rpx 0 60rpx;
		.account_info {
			padding-bottom: 60rpx;
			.account_info_title {
				font-size: 36rpx;
				color: #1A1A1A;
				padding-bottom: 30rpx;
			}
			.account_info_input {
				font-size: 28rpx;
				padding-bottom: 15rpx;
				color: #999;
				border-bottom: 1px solid #F3f3f3;
			}
		}
		.register_btn {
			margin-top: 30rpx;
		}
		.captcha {
			font-size: 24rpx;
			width: 150rpx;
			height: 42rpx;
			line-height: 42rpx;
			border-radius: 10rpx;
			color: #fff;
			right: 0;
			bottom: 80rpx;
			background-color: #D1A764;
		}
	}
</style>
